#include <iostream>
using std::cout;
using std::endl;

class Point{
    //C++11提供的写法，给出一个默认的无参构造
    Point() = default;

    Point(int x,int y)
    : _ix(x) 
    , _iy(y) 
    {
        cout <<"Point(int,int)" << endl;
    }
public:
    static Point * getInstance(){
        if(_pInstance == nullptr){
            _pInstance = new Point(1,2);
        }
        return _pInstance;
    }

    void init(int x,int y){
        _ix = x;
        _iy = y;
    }

//意味着禁止进行复制
    Point(const Point & rhs) = delete;
    Point & operator=(const Point & rhs) = delete;
#if 0
private:
    Point(const Point & rhs)
    : _ix(rhs._ix)
    , _iy(rhs._iy)
    {
        cout << "Point(const Point & )" << endl;
    }

    Point & operator=(const Point & rhs){
        _ix = rhs._ix;
        _iy = rhs._iy;
        cout <<"Point & operator=" << endl;
        return *this;
    }
#endif

public:
    void print(){
        cout << "(" << _ix << ","
            << _iy << ")" << endl;
    }

    static void destroy(){
        if(_pInstance){
            delete _pInstance;
            _pInstance = nullptr;
        }
    }
private:
    ~Point(){
        cout << "~Point()" << endl;
    }
private:
    int _ix;
    int _iy;
    static Point * _pInstance;
};
Point * Point::_pInstance = nullptr;

#if 0
void test0(){
    /* Point pt(1,2); */
    /* Point pt2(3,4); */
    Point * pt = Point::getInstance();
    Point * pt2 = Point::getInstance();
    cout << pt << endl;
    cout << pt2 << endl;
    pt->print();
    pt2->print();
    pt->destroy();
}
#endif

void test1(){
    //单例模式中使用单例对象更常见的写法
    Point::getInstance()->init(5,9);
    Point::getInstance()->print();
    Point::getInstance()->init(10,19);
    Point::getInstance()->print();
    Point::destroy();
    /* Point::getInstance()->destroy(); */
}

int main(void){
    /* test0(); */
    test1();
    return 0;
}
